package com.superman.supermarket.client.composite.storecoupon;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ToastEvent;
import com.superman.mvpframe.client.gin.XGinjector;
import com.superman.supermarket.client.composite.IUIModule;
import com.superman.supermarket.client.constant.StoreCouponConstants;

/**
 * 店铺优惠券
 * 
 * @author superman
 * @version 2018年8月18日下午11:40:23
 */
public class StoreCouponModule extends Composite implements IUIModule {

	interface StoreCouponModuleUiBinder extends UiBinder<Widget, StoreCouponModule> {
	}

	private static StoreCouponModuleUiBinder uiBinder = GWT.create(StoreCouponModuleUiBinder.class);

	private boolean canGet;
	@UiField
	protected HTML namehtml, limithtml, timehtml, getbtn;
	private String planId;
	@UiField
	protected FlowPanel wrapperpanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月18日 下午11:40:21
	 */
	public StoreCouponModule() {
		initWidget(uiBinder.createAndBindUi(this));

	}

	private void changeGetBtn() {
		if (canGet) {
			getbtn.getElement().getStyle().clearColor();
			getbtn.addStyleName("base-color");
			getbtn.setText("领取");
		} else {
			getbtn.getElement().getStyle().clearColor();
			getbtn.removeStyleName("base-color");
			getbtn.getElement().getStyle().setProperty("color", "#999");
			getbtn.setText("已领取");
		}
	}

	@UiHandler("getbtn")
	void getCoupon(ClickEvent event) {
		if (canGet) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("PLAN_ID", planId);
			XGinjector.INSTANCE.getPostMan().post("/coupon/get", map, new PostManAsyncCallBack() {

				@Override
				public void onBusinessError(String statusCode) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onBusinessOK(String response) {
					XGinjector.INSTANCE.getEventBus().fireEvent(new ToastEvent("已领取"));
					canGet = false;
					changeGetBtn();
				}
			});
		}
	}

	@Override
	public Map<String, Object> getData() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setData(Map<String, Object> map) {
		this.planId = String.valueOf(map.get("PLAN_ID"));
		this.canGet = (int) Double.parseDouble(String.valueOf(map.get("CAN_GET"))) == 1;
		String nameStr = String.valueOf(map.get("NAME"));
		namehtml.setText(nameStr);

		// 限制类型
		int limitType = (int) Double.parseDouble(String.valueOf(map.get("LIMIT_TYPE")));
		limithtml.setText(StoreCouponConstants.getLimitDescByType(limitType));

		DateTimeFormat sdtf = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm:ss");
		DateTimeFormat edtf = DateTimeFormat.getFormat("yyyy-MM-dd");
		String startTime = String.valueOf(map.get("START_DATE"));
		String endTime = String.valueOf(map.get("END_DATE"));
		String timeStr = edtf.format(sdtf.parse(startTime)) + "至" + edtf.format(sdtf.parse(endTime));
		timehtml.setText(timeStr);

		changeGetBtn();
	}

}
